you will learn how to upload an image to database and show it in your pages


=================================================


//<!----- Upload.php File ------>//
<?PHP
include("connect.php");
if(isset($_FILES['file'])){
	$array = array("image/gif","image/jpeg","image/png","image/bmp");
	$name = $_FILES['file']['name'];
	$type = $_FILES['file']['type'];
	$new_path = "images/$name";
	$path = $_FILES['file']['tmp_name'];
	if(!in_array($type,$array)){
	die("Not an image file");
	}
	if(!file_exists($new_path)){
		if(!move_uploaded_file($path,$new_path)){
			die("Error Uploading File");
		}
	}

	$fp = fopen($new_path,"rb");
	$fread = fread($fp,filesize($new_path));
	fclose($fp);
	@unlink($new_path);
	$encode = chunk_split(base64_encode($fread));
	$chk_query = mysqli_query($con,"SELECT * FROM $tblname WHERE `image`='$encode'")or die(mysqli_error($con));
	if(mysqli_num_rows($chk_query)==0){
	$img_str = "INSERT INTO $tblname (`name`,`image`) VALUE ('$name','$encode')";
	$img_query = mysqli_query($con,$img_str)or die(mysqli_error($con));
	echo "Image Uploaded";	
	}
	else{
	echo "Image Already Exists";
	}
}
?>
<form action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data" id="form1">
  <label>
  <input type="file" name="file" />
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
</form>
<p><a href="show.php"></a></p>

<?PHP
$show = mysqli_query($con,"SELECT * FROM `images`")or die(mysqli_error($con));
while($result = mysqli_fetch_assoc($show)){
	echo "<a href='show.php?id=".$result['id']."' >$result[name]</a><br />";
}
?>

//<!------------------ select.php file (this code is printing image) ---->//
<?php
include("connect.php");
	$img_str = "SELECT * FROM $tblname WHERE id=".$_GET['id'];
	$img_query = mysqli_query($con,$img_str)or die(mysqli_error($con));
	$img_fetch = mysqli_fetch_assoc($img_query);
	echo base64_decode($img_fetch['image']);
?>


//<!-------- img.php show image from select.php file ------------->//
<?PHP
echo "<img src='select.php?id=".$_GET['id']."' />";
?>